package com.letv.whatslive.model.convert;

import com.letv.whatslive.model.GlobalResource;
import com.letv.whatslive.model.GlobalSwitch;
import com.letv.whatslive.model.utils.ObjUtils;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import org.apache.commons.lang3.StringUtils;

//2015.11.4

/**
 * Created by shenyue on 2015/11/4.
 */
public class GlobalSwitchConvert {
    public static DBObject castGlobalSwitchToDBObject(GlobalSwitch globalSwitch){
        DBObject dbo = new BasicDBObject();
        if(globalSwitch.getId()!=null){
            dbo.put("_id",globalSwitch.getId());
        }

        if(globalSwitch.getSwitchType()!=null){
            dbo.put("switchType",globalSwitch.getSwitchType());
        }else{
            dbo.put("switchType",0);
        }
        if(globalSwitch.getSpecialType()!=null){
            dbo.put("specialType",globalSwitch.getSpecialType());
        }else{
            dbo.put("specialType",0);
        }
        if(globalSwitch.getSwitchStatus()!=null){
            dbo.put("switchStatus",globalSwitch.getSwitchStatus());
        }else{
            dbo.put("switchStatus",0);
        }
        if(StringUtils.isNotBlank(globalSwitch.getSwitchDescribe())){
            dbo.put("switchDescribe",globalSwitch.getSwitchDescribe());
        }else{
            dbo.put("switchDescribe","");
        }
        if(StringUtils.isNotBlank(globalSwitch.getAppVer())){
            dbo.put("appVer",globalSwitch.getAppVer());
        }else{
            dbo.put("appVer","");
        }
        return dbo;


//        DBObject dbo = new BasicDBObject();
//        if (device.getId() != null) {
//            dbo.put("_id", device.getId());
//        }
//        dbo.put("platformId",device.getPlatformId());
//        dbo.put("sysVer", device.getSysVer());
//        dbo.put("model", device.getModel());
//        dbo.put("appVer", device.getAppVer());
//        dbo.put("corporationId", device.getCorporationId());
//        dbo.put("channelId", device.getChannelId());
//        return dbo;
    }

    public static GlobalSwitch castDBObjectToGlobalSwitch(DBObject dbObject){
        GlobalSwitch globalSwitch = new GlobalSwitch();
        if(dbObject != null) {
            globalSwitch.setId(ObjUtils.toLong(dbObject.get("_id")));
            globalSwitch.setSwitchDescribe(ObjUtils.toString(dbObject.get("switchDescribe")));
            globalSwitch.setSwitchType(ObjUtils.toInteger(dbObject.get("switchType")));
            globalSwitch.setSpecialType(ObjUtils.toInteger(dbObject.get("specialType")));
            globalSwitch.setSwitchStatus(ObjUtils.toInteger(dbObject.get("switchStatus")));
            globalSwitch.setAppVer(ObjUtils.toString(dbObject.get("appVer"),""));

        }

        return globalSwitch;
    }
}
